package com.project.javagobang.api;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
@Component
public class TestAPI extends TextWebSocketHandler {
    /**
     * afterConnectionEstablished 连接建立之后触发这个方法,感知到客户端建立连接或者用户上线
     */
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        System.out.println("连接成功");
    }
    /**
     *handleTextMessage 如果客户端给服务器发送消息,感知消息内容
     */

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        System.out.println("收到消息:"+message.getPayload());
        //让服务器收到数据之后,把数据原封不动的返回回去
        session.sendMessage(message);
    }
    /**
     *handleTransportError 如果连接失败产生异常(网络波动等等),触发该函数,说明连接出现了问题
     */
    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        System.out.println("连接异常");
    }
    /**
     *afterConnectionClosed 连接被关闭,执行该函数
     */
    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        System.out.println("连接关闭");
    }
}
